package com.wdc.n1;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SaleTicketDemo02  {



    public static void main(String[] args) {

        Ticket2 ticket2 = new Ticket2();


            new Thread(() -> {for (int i = 0; i < 40; i++) ticket2.sale();}, "A").start();
            new Thread(() -> {for (int i = 0; i < 40; i++) ticket2.sale();}, "B").start();
            new Thread(() -> {for (int i = 0; i < 40; i++) ticket2.sale();}, "C").start();

    }



}

class Ticket2 {
    private int number = 30;

    Lock lock = new ReentrantLock();


    public  void sale() {

        lock.lock();

        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + " 卖出了第 " + number-- + " 票，剩余" + number);

            }
        }finally {
            lock.unlock();

        }


    }
}
